package com.revenant.driver.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.nanchen.compresshelper.CompressHelper;
import com.revenant.driver.R;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.ShipperBean.FileUploadBean;
import com.revenant.driver.utils.GlideEngine;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.revenant.driver.utils.Constants.UploadFile;

public class DriverRenZhengOneActivity extends BaseActivity {
    @BindView(R.id.upload_text_type)
    TextView  uploadTextType;
    @BindView(R.id.upload_second_title)
    TextView  uploadSecondTitle;
    @BindView(R.id.upload_photo)
    ImageView uploadPhoto;
    @BindView(R.id.upload_type)
    Button    uploadType;
    private String Name_header;

    private Context          context;
    private List<LocalMedia> selectList = new ArrayList<>();
    private String           uploadurl  = "";

    @Override
    public int getContentViewResId() {
        context = this;
        return R.layout.activity_driver_one;
    }

    @Override
    public void initView() {
        setcenterTitle("完善司机信息");

    }

    @Override
    public void initData() {

    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.upload_photo, R.id.upload_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.upload_photo:
                showBottomSheetDialog(DriverRenZhengOneActivity.this);
//                PictureSelector.create(this)
//                        .openGallery(PictureMimeType.ofImage())
//                        .maxSelectNum(1)
//                        .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
//                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.upload_type:
                if (!uploadurl.trim().isEmpty()) {
                    Bundle bundlea = new Bundle();
                    bundlea.putString("upload_type_fileurl", uploadurl);
                    finish();
                    startActivity(DriverRenZhengTwoActivity.class, bundlea);

                } else {
                    showToast("请选择图片");
                }
//                Bundle bundlea = new Bundle();
//                bundlea.putString("upload_type_fileurl", uploadurl);
//                startActivity(DriverRenZhengTwoActivity.class, bundlea);
//                finish();

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia media : selectList) {
                        if (!selectList.isEmpty()) {
//                            uploadAvatar(media.getPath());
                            String url = media.getPath().startsWith("content://") ? media.getAndroidQToPath() : media.getPath();
                            uploadAvatar(url);
                        }
                    }
                    break;
            }
        }
    }


    private void uploadAvatar(final String path) {
        showDig();
        File file = new File(path);
        File newfile = CompressHelper.getDefault(context).compressToFile(file);

        OkGo.<String>post(UploadFile)
                .tag(this)
                .isMultipart(true)

                .params("file", newfile)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                     if (response.code()==200){
                         FileUploadBean fileUploadBean = GsonUtil.parseJsonWithGson(response.body(), FileUploadBean.class);
                         if (fileUploadBean.getCode().equals("0")) {
                             uploadurl = fileUploadBean.getData().getUrl();
                             Glide.with(context).load(fileUploadBean.getData().getUrl()).centerCrop().
                                     into(uploadPhoto);
                             dismissDig();
                         } else {
                             showToast(fileUploadBean.getMsg());
                             dismissDig();
                         }
                     }else {
                         showToast(response.message());
                     }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        LogUtils.d("失败是" + response.message());
                        showToast("图片上传失败");
                        dismissDig();
                    }
                });
    }
}
